package com.pet.service.impl;

import com.pet.mapper.SysSmallDogMapper;
import com.pet.pojo.SysSmallDog;
import com.pet.pojo.Result;
import com.pet.service.ISysSmallDogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysSmallDogServiceImpl implements ISysSmallDogService {

    @Autowired
    private SysSmallDogMapper smallDogMapper;

    @Override
    public Result findByCondition(Map<String, Object> params) {
        // 获取总数
        int total = smallDogMapper.countByCondition(params);
        if (total == 0) {
            return new Result("200", "暂无数据", new HashMap<String, Object>() {
                {
                    put("total", 0);
                    put("list", List.of());
                }
            });
        }

        // 获取列表
        List<SysSmallDog> smallDogs = smallDogMapper.findByCondition(params);

        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", smallDogs);

        return new Result("200", "获取成功", result);
    }

    @Override
    public Result findById(Integer id) {
        SysSmallDog smallDog = smallDogMapper.findById(id);
        return smallDog != null ? new Result("200", "获取成功", smallDog) : new Result("404", "小型犬不存在");
    }

    @Override
    @Transactional
    public Result add(SysSmallDog smallDog) {
        try {
            int rows = smallDogMapper.insert(smallDog);
            return rows > 0 ? new Result("200", "添加成功") : new Result("500", "添加失败");
        } catch (Exception e) {
            return new Result("500", "添加失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result update(SysSmallDog smallDog) {
        try {
            int rows = smallDogMapper.update(smallDog);
            return rows > 0 ? new Result("200", "更新成功") : new Result("500", "更新失败，小型犬可能不存在");
        } catch (Exception e) {
            return new Result("500", "更新失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result delete(Integer id) {
        try {
            int rows = smallDogMapper.delete(id);
            return rows > 0 ? new Result("200", "删除成功") : new Result("500", "删除失败，小型犬可能不存在");
        } catch (Exception e) {
            return new Result("500", "删除失败：" + e.getMessage());
        }
    }
} 